//
//  FenceModel.swift
//  LocationTrackerAdmin
//
//  Created by mac on 2025/2/25.
//  地理围栏数据模型

import Vapor
import Fluent
import SQLKit
final class TaskModel: Model, Content {
    static var schema: String {
        return "tasks"
    }
    
    @ID(custom: FieldKeys.id)
    var id: String?
    
    @Field(key: FieldKeys.initiatorID)
    var initiatorID: String
    
    @Field(key: FieldKeys.monitoredID)
    var monitoredID: String
    
    @Field(key: FieldKeys.createDate)
    var createDate: Date
    
    @Field(key: FieldKeys.groupId)
    var groupId: String?
    
    @Field(key: FieldKeys.updateDate)
    var updateDate: Date
    
    @OptionalField(key: FieldKeys.expiryDate)
    var expiryDate: Date?
    
    @Field(key: FieldKeys.status)
    var status: Int// 0失效 1生效 2完成

    @Field(key: FieldKeys.type)
    var type: Int// 0越界设置 1进入区间 2未达 3事故监测 4SOS
    
    @Field(key: FieldKeys.collissionOccured)
    var collisionOccured: Bool
    
    struct FenceData: Content {
        struct FenceLocation: Content {
            let id: String
            let longitude: Double
            let latitude: Double
            let detail: String
        }
        let id: String
        let type: Int // 0 rectangle 1 circle 2 任意
        let locations: [FenceLocation]
        let remark: String?
    }
    @Field(key: FieldKeys.fence)
    var fenceData: [FenceData]?
    
    struct LocationWhenTaskOccured: Content {
        let latitude: Double
        let longitude: Double
        let detail: String
        let date: Date
    }
    @Field(key: FieldKeys.locationsWhenTaskOccured)
    var locationsWhenTaskOccured: [LocationWhenTaskOccured]?
    
    @Field(key: FieldKeys.readedUsers)
    var readedUsers: [String]
    
    init(id: String? = nil, initiatorID: String, monitoredID: String, createDate: Date, groupId: String? ,updateDate: Date, expiryDate: Date? = nil, status: Int, type: Int, fenceData: [FenceData]? = nil, collisionOccured: Bool = false, locationsWhenTaskOccured: [LocationWhenTaskOccured]?, readedUsers: [String] = []) {
        self.id = id
        self.initiatorID = initiatorID
        self.monitoredID = monitoredID
        self.createDate = createDate
        self.updateDate = updateDate
        self.expiryDate = expiryDate
        self.status = status
        self.type = type
        self.fenceData = fenceData
        self.collisionOccured = collisionOccured
        self.readedUsers = readedUsers
        self.groupId = groupId
        self.locationsWhenTaskOccured = locationsWhenTaskOccured
    }
    
    init() {
        
    }
    
}

extension TaskModel {
    struct FieldKeys {
        static var id: FieldKey { "task_id" }
        static var initiatorID: FieldKey { "task_initiator_id" }
        static var monitoredID: FieldKey { "task_monitored_id" }
        static var expiryDate: FieldKey { "task_expiry_date" }
        static var type: FieldKey { "task_type" }
        static var fence: FieldKey { "task_fence" }
        static var createDate: FieldKey { "task_create_date" }
        static var updateDate: FieldKey { "task_update_date" }
        static var status: FieldKey { "task_status" }
        static var collissionOccured: FieldKey { "task_collission_occured"}
        static var readedUsers: FieldKey { "task_readed_users"}
        static var groupId: FieldKey { "task_group_id"}
        static var locationsWhenTaskOccured: FieldKey { "task_locations_when_task_occured"}
    }
}

struct TaskModelMigration: AsyncMigration {
    func prepare(on database: Database) async throws {
        try await database.schema(TaskModel.schema)
            .field(TaskModel.FieldKeys.id, .string, .custom("COMMENT '任务ID'"))
            .field(TaskModel.FieldKeys.initiatorID, .string, .required, .custom("COMMENT '发起人ID'"))
            .field(TaskModel.FieldKeys.groupId, .string, .custom("COMMENT '所属群组ID'"))
            .field(TaskModel.FieldKeys.monitoredID, .string, .required, .custom("COMMENT '被监控人ID'"))
            .field(TaskModel.FieldKeys.locationsWhenTaskOccured, .json, .custom("COMMENT '该事件发生时用户所在的地理位置以及时间'"))
            .field(TaskModel.FieldKeys.expiryDate, .custom("datetime(0)"), .custom("COMMENT '过期时间'"))
            .field(TaskModel.FieldKeys.createDate, .custom("datetime(0)"), .required, .sql(.default(SQLFunction("now"))), .custom("COMMENT '创建日期'"))
            .field(TaskModel.FieldKeys.updateDate, .custom("datetime(0)"), .required, .sql(.default(SQLFunction("now"))), .custom("COMMENT '修改日期'"))
            .field(TaskModel.FieldKeys.status, .int, .required, .custom("COMMENT '状态 1就绪 2已触发 3已结束 '"))
            .field(TaskModel.FieldKeys.type, .int, .required, .custom("COMMENT '任务类型'"))
            .field(TaskModel.FieldKeys.fence, .json, .custom("COMMENT '围栏数据'"))
            .field(TaskModel.FieldKeys.collissionOccured, .bool, .sql(.default(false)), .custom("COMMENT '是否碰撞'"))
            .field(TaskModel.FieldKeys.readedUsers, .json, .custom("COMMENT '已读用户记录'"))
            .create()
    }
    
    func revert(on database: Database) async throws {
        try await database.schema(TaskModel.schema).delete()
    }
}

